const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../services/user.js');
const app = getApp();

Page({
  data: {
    userInfo: {},
    showLoginDialog: false
  },
  onLoad: function() {
    var that=this;
    console.log(that.data.brandid);
    console.log('wx.getStorageSync(userInfo)',wx.getStorageSync('userInfo'));
    setTimeout(()=>{
      wx.cloud.callFunction({
        name: 'login',
        data: {
        }
      }).then(res => {
        if(res.result.data.length>0){
          console.log('res: ', res)
          let userInfo= res.result.data[0]
          app.globalData.userInfo = userInfo;
          // wx.setStorageSync('userInfo', JSON.stringify(userInfo));
          console.log('userInfo: ', userInfo)
          console.log('userInfo: ', userInfo.avatarUrl)
          that.setData({
            userInfo: userInfo,
            showLoginDialog: false
            
          })
        }else{
          that.setData({
            isShowBind:true
          })
        }
      }) 
    },1000)
  },
  onReady: function() {

  },
  onShow: function() {
    this.setData({
      userInfo: app.globalData.userInfo,
    });
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭
  },

  onUserInfoClick: function() {
    if (wx.getStorageSync('userInfo')) {

    } else {
      this.showLoginDialog();
    }
  },

  showLoginDialog() {
    this.setData({
      showLoginDialog: true
    })
  },

  onCloseLoginDialog () {
    this.setData({
      showLoginDialog: false
    })
  },

  onDialogBody () {
    // 阻止冒泡
  },

  onWechatLogin(e) {
    const that = this
    console.log("register")
    if (e.detail.errMsg !== 'getUserInfo:ok') {
      if (e.detail.errMsg === 'getUserInfo:fail auth deny') {
        wx.showToast({
          title: '微信登录失败',
        })
        return false
      }
      // wx.showToast({
      //   title: '微信登录失败',
      // })
      // return false
    }
    console.log('e.detail: ', e)
    wx.cloud.callFunction({
      name: 'register',
      data: e.detail.userInfo
    }).then(res => {
      console.log('res: ', res)
      that.setData({
        userInfo: e.detail.userInfo,
        showLoginDialog: false,
      })
      app.globalData.userInfo = res;
      wx.setStorageSync('userInfo', res);
    })
  },

  onOrderInfoClick: function(event) {
    wx.navigateTo({
      url: '/pages/ucenter/order/order',
    })
  },

  onSectionItemClick: function(event) {

  },

  // TODO 移到个人信息页面
  exitLogin: function() {
    let that=this
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '退出登录？',
      success: function(res) {
        if (res.confirm) {
          wx.removeStorageSync('userInfo');
          // wx.switchTab({
          //   url: '/pages/index/index'
          // });
          app.globalData.userInfo = {
            nickname: '点击登录',
            avatar: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
          }
          that.setData({
            userInfo: {
              nickname: '点击登录',
              avatar: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
            }
          })
        }
      }
    })

  }
})